/*
 * ChannelFactory.java
 * 
 * Created on Jun 9, 2007, 10:09:30 AM
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.infomancers.workflow.channels;

import com.infomancers.workflow.Token;
import java.io.IOException;
import java.util.concurrent.Executor;

/**
 *
 * @author aviadbd
 */
public abstract class ChannelFactory {
    private final class FactoryInputChannel extends InputChannel {
        protected FactoryInputChannel() {
            super(exec, null);
        }
        
        protected void send(Token token) throws IOException {
            internalSend(token);
        }

        protected void init() throws IOException {
            initInputChannel();
        }
    }
    
    private final class FactoryOutputChannel extends OutputChannel {
        protected FactoryOutputChannel() {
            super(exec);
        }
        
        protected Token retrieve() throws IOException {
            return internalRetrieve();
        }

        protected void init() throws IOException {
            initOutputChannel();
        }
    }
    
    
    private Executor exec;
    
    public ChannelFactory(Executor exec) {
        this.exec = exec;
    }
    
    protected abstract void internalSend(Token token) throws IOException;
    protected abstract Token internalRetrieve() throws IOException;
    protected abstract void initInputChannel() throws IOException;
    protected abstract void initOutputChannel() throws IOException;
    
    public final InputChannel createInput() {
        return new FactoryInputChannel();
    }
    
    public final OutputChannel createOutput() {
        return new FactoryOutputChannel();
    }
    
    protected final Executor getExecutor() {
        return exec;
    }
}
